<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" width="1000px">
    <AdTable @register="registerTable" size="small" :searchInfo="filterGroupRef">
      <template #toolbar>
        <Authority :value="''">
          <a-button type="primary" @click="handleCreate">
            <Icon icon="ant-design:plus-circle-outlined" />新增
          </a-button>
        </Authority>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :stop-button-propagation="true"
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                auth: '', // 权限控制
                tooltip: '编辑',
                ifShow: (_action) => {
                  return true;
                },
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除',
                auth: '', // 权限控制
                ifShow: (_action) => {
                  return true;
                },
                popConfirm: {
                  title: '确认删除？',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </AdTable>
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '@/hooks/web/useI18n';

  import { BasicDrawer, useDrawerInner } from '@/components/Drawer';
  import { AdTable, useAdTable, TableAction } from '@/components/AdTable';
  import { productCaseColumns } from './product.data';
  import { useMessage } from '@/hooks/web/useMessage';
  import { AjaxResult } from '@/api/model';
  import { AjaxResultTypeEnum } from '@/enums/ajaxResultTypeEnum';
  import Icon from '@/components/Icon/Icon.vue';
  import { Authority } from '@/components/Authority';

  import { httpClient } from '@/utils/http/axios';
  import { ErrorMessageMode } from '#/axios';
  import { FunctionOutputDtoSimple } from '@/api/authorization/admin/function/model';
  import { PageRequest, PageData, FilterRule, FilterGroup } from '@/utils/common/types';
  import { FilterOperateEnum } from '@/enums/filterOperateEnum';
  import { FormModeEnum } from '@/enums/formEnum';
  import {
    ReadProductCase,
    CreateProductCase,
    UpdateProductCase,
    DeleteProductCase,
  } from './product.api';

  export default defineComponent({
    name: 'UserFunctionDrawer',
    components: { BasicDrawer, AdTable, TableAction, Icon, Authority },
    setup() {
      const { t } = useI18n();
      const { createMessage } = useMessage();
      const filterGroupRef = ref<FilterGroup>(new FilterGroup());
      const productId = ref<string>('');

      const [registerTable, { openDetailForm, reload }] = useAdTable({
        // 表标题呈现器
        title: '案列列表',
        // 表格数据接口请求对象
        api: ReadProductCase,
        // 表单列信息 BasicColumn[]
        columns: productCaseColumns,
        // 是否可拖拽列
        canColDrag: false,
        // 使用搜索表单
        useSearchForm: true,
        // 显示表格设置工具
        showTableSetting: true,
        //表格设置项
        tableSetting: { redo: true, size: true, setting: false, fullScreen: false },
        // 是否显示表格边框
        bordered: false,
        // 是否显示序号列
        showIndexColumn: true,
        // 详情表单抬头
        detailForm: {
          // 表单标识
          formIndex: 'brand_productcase_detail',
          // 主键字段
          keyField: 'Id',
          title: '产品详情',
          createApi: CreateProductCase,
          updateApi: UpdateProductCase,
          deleteApi: DeleteProductCase,
        },
      });

      const [registerDrawer, { setDrawerProps }] = useDrawerInner(async (data) => {
        setDrawerProps({ destroyOnClose: true, confirmLoading: false });
        filterGroupRef.value.Rules = [];
        if (data.record) {
          productId.value = data.record.Id;
          filterGroupRef.value.Rules.push(
            new FilterRule('ProductId', `${data.record.Id}`, FilterOperateEnum.Equal),
          );
        }
        await reload();
      });

      async function handleCreate() {
        openDetailForm(FormModeEnum.Add, { ProductId: productId.value });
      }

      async function handleEdit(record: Recordable) {
        openDetailForm(FormModeEnum.Edit, {
          ...record,
          //ProductId: productId.value
        });
      }

      /** 处理删除 */
      async function handleDelete(record: Recordable) {
        const ajaxResult = await DeleteProductCase([record.Id]);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          reload();
        }
      }

      return {
        t,
        registerDrawer,
        registerTable,
        handleDelete,
        filterGroupRef,
        handleCreate,
        handleEdit,
      };
    },
  });
</script>
